package ebiz.buddhist.texts;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Toast;

public class mpDelete extends Activity {

	NotificationManager mNotiManager;
	ArrayList<String> mList;
	ArrayList<String> mListItem;
	ArrayAdapter<String> Adapter;


	ListView list;
	Cursor mCursor;
	
	
	String [] tmpSaveFile;
	Button mPlayBtn;
	SeekBar mProgress;
	
	boolean wasPlaying;
	boolean isInitCheck = false;

	
	static final int NAPNOTI = 1;
	Handler mHandler;// = new Handler(); 
	boolean mRunning; //= false; // sleep timer
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mpdelete);
		mList = new ArrayList<String>();
		mListItem =  searching();
		list = (ListView)findViewById(R.id.list);
		mHandler = new Handler();	
	    mRunning = false;
	
		Adapter = new ArrayAdapter<String>(this, 
				android.R.layout.simple_list_item_multiple_choice, mListItem);
		list.setAdapter(Adapter);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		list.setOnItemClickListener(mItemClickListener);
				
		// 버튼들의 클릭 리스너 등록
		mPlayBtn = (Button)findViewById(R.id.play);
	}
	 public String mpReNameHan(String str)
	 {
		 String strCompare = str;
		   if(strCompare.compareTo("chunsu.mp3")== 0)
			   strCompare = getString(R.string.item_chunsu);
		   if(strCompare.compareTo("heartsutra.mp3")== 0)
			   strCompare = getString(R.string.item_sutra);
		   if(strCompare.compareTo("beopseong.mp3")== 0)
			   strCompare = getString(R.string.item_beopseong);
		   if(strCompare.compareTo("hwaeom.mp3")== 0)
			   strCompare = getString(R.string.item_hwaeom);
		   if(strCompare.compareTo("sinju.mp3") == 0)
			   strCompare = getString(R.string.item_sinju);
//---------------------------------------------------------
		   if(strCompare.compareTo("kanmu.mp3") == 0)
			   strCompare = getString(R.string.item_kanmu);
		   if(strCompare.compareTo("bodalbomun.mp3") == 0)
			   strCompare = getString(R.string.item_bodalbomun);
		   if(strCompare.compareTo("kanja.mp3") == 0)
			   strCompare = getString(R.string.item_kanja);		  
//---------------------------------------------------------
		   
		   if(strCompare.compareTo("gwaneum.mp3") == 0)
			   strCompare = getString(R.string.item_gwaneum);
		   if(strCompare.compareTo("ami.mp3") == 0)
			   strCompare = getString(R.string.item_ami);				   
		   if(strCompare.compareTo("kukrak.mp3") == 0)
			   strCompare = getString(R.string.item_kukrak);		  
		   if(strCompare.compareTo("musang.mp3") == 0)
			   strCompare = getString(R.string.item_musang);
		   if(strCompare.compareTo("kang1.mp3") == 0)
			   strCompare = getString(R.string.item_kang1);
		   if(strCompare.compareTo("kang2.mp3") == 0)
			   strCompare = getString(R.string.item_kang2);
		   if(strCompare.compareTo("kang3.mp3") == 0)
			   strCompare = getString(R.string.item_kang3);
		   if(strCompare.compareTo("kang4.mp3") == 0)
			   strCompare = getString(R.string.item_kang4);
		   if(strCompare.compareTo("han_heartsutra.mp3") == 0)
			   strCompare = getString(R.string.item_kor_heartsutra);
		   if(strCompare.compareTo("han_chunsu.mp3") == 0)
			   strCompare = getString(R.string.item_kor_chunsu);
		   if(strCompare.compareTo("chunji.mp3") == 0)
			   strCompare = getString(R.string.item_chunji);
		   if(strCompare.compareTo("sinmyo.mp3") == 0)
			   strCompare = getString(R.string.item_sinmyo);
		   if(strCompare.compareTo("yebull.mp3")==0)
			   strCompare = getString(R.string.item_yebull);
		   if(strCompare.compareTo("prajna.mp3")==0)
			   strCompare = getString(R.string.item_prajna);
		   
	/*
	 * sekisuri 20140618
		   if(strCompare.compareTo("108_7.mp3") == 0)
			   strCompare = getString(R.string.item_bows07);
		   if(strCompare.compareTo("108_8.mp3") == 0)
			   strCompare = getString(R.string.item_bows08);
		   if(strCompare.compareTo("108_9.mp3") == 0)
			   strCompare = getString(R.string.item_bows09);
		   if(strCompare.compareTo("108_10.mp3") == 0)
			   strCompare = getString(R.string.item_bows10);
		   if(strCompare.compareTo("108_11.mp3") == 0)
			   strCompare = getString(R.string.item_bows11);
		   if(strCompare.compareTo("108bow.mp3") == 0)
			   strCompare = getString(R.string.item_bows_s);
		   if(strCompare.compareTo("108bow_youngin.mp3") == 0)
			   strCompare = getString(R.string.item_bows_youngin);
	*/	   
		   
		   if(strCompare.compareTo("MedicineBuddha.mp3") == 0)
			   strCompare = getString(R.string.item_medicine_buddha);

		   if(strCompare.compareTo("MantraOfBodhisattvaCundi.mp3") == 0)
		   strCompare = getString(R.string.cundi);

		   if(strCompare.compareTo("Monks_of_Tibet_Healing.mp3") == 0)
		   strCompare = getString(R.string.monks);

		   if(strCompare.compareTo("Korean_Zen_master.mp3") == 0)
		   strCompare = getString(R.string.zen_master);

		   
		   if(strCompare.compareTo("chant.mp3") == 0)
		   strCompare = getString(R.string.item_chant);

		   if(strCompare.compareTo("followyourheart.mp3") == 0)
		   strCompare = getString(R.string.item_followyourheart);

		   if(strCompare.compareTo("windy_hermitage.mp3") == 0)
		   strCompare = getString(R.string.item_windy_hermitage);

		   if(strCompare.compareTo("healing_music.mp3") == 0)
		   strCompare = getString(R.string.item_healing_music);
		 
		   if(strCompare.compareTo("Buddhist_hymn3.mp3") == 0)
			   strCompare = getString(R.string.Buddhist_hymn_3);
		 
		
		   if(strCompare.compareTo("sakyamuni.mp3") == 0)
			   strCompare = getString(R.string.item_sakyamuni);
		 
		   if(strCompare.compareTo("guan.mp3") == 0)
			   strCompare = getString(R.string.item_guan);
		   
		   if(strCompare.compareTo("diamond_sutra.mp3") == 0)
			   strCompare = getString(R.string.item_diamond);
		   
		   if(strCompare.compareTo("jeongmok_1.mp3") == 0)
			   strCompare = getString(R.string.mp3_meditation);
		   
		   if(strCompare.compareTo("jeongmok_2.mp3") == 0)
			   strCompare = getString(R.string.mp3_meditation_2);
		   
		   if(strCompare.compareTo("semin_hwaeom.mp3") == 0)
			   strCompare = getString(R.string.item_semin2);
		   if(strCompare.compareTo("semin_valwon.mp3") == 0)
			   strCompare = getString(R.string.mp_valwon);
		   
		   

		   return strCompare;
	 }
	 public String mpReNameEn(String str)
	 {
		 String strCompare = str;
		   if(strCompare.compareTo(getString(R.string.item_chunsu))== 0)
			   strCompare = "chunsu.mp3";
		   if(strCompare.compareTo(getString(R.string.item_sutra))== 0)
			   strCompare = "heartsutra.mp3";
		   if(strCompare.compareTo(getString(R.string.item_beopseong))== 0)
			   strCompare = "beopseong.mp3";
		   if(strCompare.compareTo(getString(R.string.item_hwaeom))== 0)
			   strCompare = "hwaeom.mp3";
		   if(strCompare.compareTo(getString(R.string.item_sinju)) == 0)
			   strCompare = "sinju.mp3";

//---------------  음질이 안좋아서 삭제
		   if(strCompare.compareTo(getString(R.string.item_kanmu)) == 0)
			   strCompare = "kanmu.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bodalbomun)) == 0)
			   strCompare = "bodalbomun.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kanja)) == 0)
			   strCompare = "kanja.mp3";	
//-----------------------------------
		   
		   if(strCompare.compareTo(getString(R.string.item_gwaneum)) == 0)
			   strCompare = "gwaneum.mp3";
		   if(strCompare.compareTo(getString(R.string.item_ami)) == 0)
			   strCompare = "ami.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_kukrak)) == 0)
			   strCompare = "kukrak.mp3";
		   if(strCompare.compareTo(getString(R.string.item_musang)) == 0)
			   strCompare = "musang.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kang1)) == 0)
			   strCompare = "kang1.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kang2)) == 0)
			   strCompare = "kang2.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kang3)) == 0)
			   strCompare = "kang3.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kang4)) == 0)
			   strCompare = "kang4.mp3";	   
		   if(strCompare.compareTo(getString(R.string.item_kor_heartsutra)) == 0)
			   strCompare = "han_heartsutra.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kor_chunsu)) == 0)
			   strCompare = "han_chunsu.mp3";
		   if(strCompare.compareTo(getString(R.string.item_chunji)) == 0)
			   strCompare = "chunji.mp3";
		   if(strCompare.compareTo(getString(R.string.item_sinmyo)) == 0)
			   strCompare = "sinmyo.mp3";
		   if(strCompare.compareTo(getString(R.string.item_yebull))==0)
			   strCompare = "yebull.mp3";
		   if(strCompare.compareTo(getString(R.string.item_prajna))==0)
			   strCompare = "prajna.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_bows07)) == 0)
			   strCompare = "108_7.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows08)) == 0)
			   strCompare = "108_8.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows09)) == 0)
			   strCompare = "108_9.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows10)) == 0)
			   strCompare = "108_10.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows11)) == 0)
			   strCompare = "108_11.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows_s)) == 0)
			   strCompare = "108bow.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows_youngin)) == 0)
			   strCompare = "108bow_youngin.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_medicine_buddha)) == 0)
			   strCompare = "MedicineBuddha.mp3";
		   if(strCompare.compareTo(getString(R.string.cundi)) == 0)
			   strCompare = "MantraOfBodhisattvaCundi.mp3";
		   if(strCompare.compareTo(getString(R.string.monks)) == 0)
			   strCompare = "Monks_of_Tibet_Healing.mp3";
		   if(strCompare.compareTo(getString(R.string.zen_master)) == 0)
			   strCompare = "Korean_Zen_master.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_chant)) == 0)
			   strCompare = "chant.mp3";
		   if(strCompare.compareTo(getString(R.string.item_followyourheart)) == 0)
			   strCompare = "followyourheart.mp3";
		   if(strCompare.compareTo(getString(R.string.item_windy_hermitage)) == 0)
			   strCompare = "windy_hermitage.mp3";
		   if(strCompare.compareTo(getString(R.string.item_healing_music)) == 0)
			   strCompare = "healing_music.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.Buddhist_hymn_3)) == 0)
			   strCompare = "Buddhist_hymn3.mp3";
		   

		   if(strCompare.compareTo(getString(R.string.item_diamond)) == 0)
			   strCompare = "diamond_sutra.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_sakyamuni)) == 0)
			   strCompare = "sakyamuni.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_guan)) == 0)
			   strCompare = "guan.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.mp3_meditation)) == 0)
			   strCompare = "jeongmok_1.mp3";
		   if(strCompare.compareTo(getString(R.string.mp3_meditation_2)) == 0)
			   strCompare = "jeongmok_2.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_semin2)) == 0)
			   strCompare = "semin_hwaeom.mp3";
		   if(strCompare.compareTo(getString(R.string.mp_valwon)) == 0)
			   strCompare = "semin_valwon.mp3";
		   
		   
		   
		   return strCompare;
	 }
	 public String FileNameSearch(String strPath)
		{
		    String path;
			File SDCardRoot = Environment.getExternalStorageDirectory(); 
	        File dir = new File(SDCardRoot + "/buddhist/");
			if(dir!=null) {
				File[] dirs = dir.listFiles();
				if(dirs!=null) {
					for(int i=0; i<dirs.length; i++) {
						
						if(dirs[i].getName().endsWith("mp3")) {
							path = dirs[i].getName();
							if(path.compareTo(strPath) == 0) 
								return dirs[i].getAbsolutePath();
						}
					}
				}
			}
			
			return  strPath;
		}
	public  ArrayList<String> searching() {
		ArrayList<String> searchList = new ArrayList<String>();		
		String strCompare=null;
		ArrayList<String> sortList = new ArrayList<String>();
		File SDCardRoot = Environment.getExternalStorageDirectory(); 
        File dir = new File(SDCardRoot + "/buddhist/");
		if(dir!=null) {
			File[] dirs = dir.listFiles();
			if(dirs!=null) {
				for(int i=0; i<dirs.length; i++) {
					if(dirs[i].getName().endsWith("mp3")) {
						
						strCompare = mpReNameHan(dirs[i].getName());
						sortList.add(strCompare);
						
					}
				}
			}
		}
		// 리스트를 배열로 변환
	     Object[] obj = sortList.toArray();
	     // API에서 Arrays.sort 참조 ( 정렬의 실체임 )
	     Arrays.sort(obj, new Comparator<Object>() {
	          public int compare(Object o1, Object o2) {
	               String t1 = (String) o1;
	               String t2 = (String) o2;
	               return t1.compareTo(t2);
	          }
	     });
	          
	     for (int i = 0; i < obj.length; i++) {
	    	 searchList.add((String) obj[i]);
	    	 }
		
		return  searchList;
	}
	  public void mOnClick(View v) {
		 if(isInitCheck == false || mList.isEmpty()== true)
		 {
		    Toast.makeText(mpDelete.this, getString(R.string.message_marking), 1).show();
		 }
		 else
		 {
			 alertDel();
		 }		
	  }

 
	  void alertDel()
	 {
			new AlertDialog.Builder(mpDelete.this)
			.setTitle(getString(R.string.dialog_alert))
			.setMessage(getString(R.string.dialog_delete))
			.setPositiveButton(getString(R.string.dialog_yes), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					mpFileDelete();		
					finish();
					startActivity(new Intent(mpDelete.this, DownAndDelManager.class));	
				}
			})				
			.setNegativeButton(getString(R.string.dialog_no), null)
			.show();
		}
		void mpFileDelete()
		{
		  File file;
			for(int i=0; i <mList.size();i++)
			{
			  file = new File(mList.get(i));
			  file.delete();
			}
		  
		}	
	   
	    
	AdapterView.OnItemClickListener mItemClickListener = 
		new AdapterView.OnItemClickListener() {

		public void onItemClick(AdapterView parent, View view, int position, long id) {
			String path;
			String strChainge = mpReNameEn(mListItem.get(position));
			String strCompareName;
			String strTmp;
			boolean isAddDelCheck = false;
			File SDCardRoot = Environment.getExternalStorageDirectory(); 
			path = SDCardRoot + "/buddhist/";
			strTmp = path+strChainge;
			if(mList.isEmpty() == true)
			{
				mList.add(path+strChainge);
				isInitCheck = true;
			}
			else
			{
				for(int i=0; i <mList.size();i++)
				{
					strCompareName = mList.get(i);
					if(strCompareName.compareTo(strTmp)==0)
					{
						ArrayList<String> tmpList = new ArrayList<String>();
						tmpList.add(strTmp);
						mList.removeAll(tmpList);
						isAddDelCheck = true;
					}
				}
				
				if(isAddDelCheck == false)
				{
					mList.add(path+strChainge);
					isInitCheck = true;
				}
			}
			if(mList.isEmpty() == true)	
				isInitCheck = false;
			
		}
	};
}
